﻿using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using YuanCode.Control.Controls;

namespace HuiZe.education.View
{
    /// <summary>
    /// AttendClass.xaml 的交互逻辑
    /// </summary>
    public partial class AttendClass : UserControl
    {
        public AttendClassViewModel AttendClassView { get; set; }
        public int BkShareId { get; set; }
        public int ControlType { get; set; } = 1;

        public AttendClass()
        {
            InitializeComponent();
            Loaded += LoginView_Loaded;
            PART_SortType_Default.IsChecked = new bool?(true);
            PART_SortType_Default.Checked += PART_SortType_Default_Checked;
            PART_SortType_Time.Checked += PART_SortType_Time_Checked;
            PART_BuildNewPackage.Click += PART_BuildNewPackage_Click;
        }

        void PART_BuildNewPackage_Click(object sender, RoutedEventArgs e)
        {
            var _locator = (ViewModelLocator)App.Current.FindResource("Locator");
            _locator.PlanLessonView.IsNewBuilding = true;
            _locator.MainView.NavChangedComand.Execute("PlanLessonView");
        }

        void PART_SortType_Time_Checked(object sender, RoutedEventArgs e)
        {
            personPopup.IsOpen = false;
            _type_sort = "time";
            Update(1);
        }

        void PART_SortType_Default_Checked(object sender, RoutedEventArgs e)
        {
            personPopup.IsOpen = false;
            _type_sort = "default";
            Update(1);
        }

        void Update(int pages)
        {
            Update(_type_sort, pages);
        }
        void Update(string type, int pages)
        {
            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
            var list = attendClass.GetMyBkResourceList(type, pages);
            PART_Title.Text = $"共有{attendClass.totalBK}个备课";

            //mySCDock.Orientation = Orientation.Horizontal;
            //mySCDock.Children.Clear();

            //foreach (var child in list.data)
            //{
            //    //StackPanel stack = new StackPanel
            //    //{

            //    //    Width = 100,
            //    //    Height = 150,
            //    //    HorizontalAlignment = HorizontalAlignment.Left,
            //    //    VerticalAlignment = VerticalAlignment.Top,
            //    //};
            //    //Image image = new Image
            //    //{
            //    //    Source = new BitmapImage(new Uri(child.cover_image)),
            //    //    //Name= item.name,
            //    //    Width = 100,
            //    //    Height = 120

            //    //};
            //    //Label lb = new Label
            //    //{
            //    //    Content = child.title,
            //    //    HorizontalAlignment = HorizontalAlignment.Center
            //    //};

            //    //stack.Children.Add(image);
            //    //stack.Children.Add(lb);

            //    mybkBookEdit book = new mybkBookEdit();
            //    book.NomalImage = child.cover_image;
            //    book.Labtext = child.title;
            //    book.Id = child.id.ToString();
            //    book.Margin = new Thickness(10, 10, 0, 0);
            //    book.Height = 260;
            //    book.Width = 200;
            //    book.ShareImg.MouseLeftButtonDown += ShareImg_MouseDown;
            //    book.DownZipUrl = child.zippath;
            //    book.DownImg.MouseLeftButtonDown += DownImg_MouseDown;
            //    book.EditImg.MouseLeftButtonDown += EditImg_MouseDown;
            //    book.DelImg.MouseLeftButtonDown += DelImg_MouseDown;
            //    book.shangkeImg.MouseLeftButtonDown += shangkeImg_MouseDown;
            //    book.UpImg.MouseLeftButtonDown += UpImg_MouseDown;

            //    mySCDock.Children.Add(book);
            //}

            PART_MyPackages_H.Load(list.data);
            PART_MyPackages_V.Load(list.data);

            pageManage.Visibility = (list.data != null && list.data.Count > 0) ? Visibility.Visible : Visibility.Collapsed;
            if (pageManage.Visibility == Visibility.Visible)
            {
                pageManage.ItemSourceCount = attendClass.pageCount;
                if (_page_count_last != attendClass.pageCount)
                    pageManage.UpdataPages();

                _page_count_last = attendClass.pageCount;
            }
        }

        //private void Update2(string type)
        //{
        //    AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
        //    var list = attendClass.GetMyBkResourceList(type, 1);
        //    //mySCDock.Orientation = Orientation.Vertical;
        //    //mySCDock.Children.Clear();
        //    //foreach (var child in list.data)
        //    //{

        //    //    mybkBookEdit4 book = new mybkBookEdit4();
        //    //    book.NomalImage = child.cover_image;
        //    //    book.Labtext = child.title;
        //    //    book.Id = child.id.ToString();
        //    //    book.Margin = new Thickness(2, 5, 0, 0);
        //    //    book.Height = 164;
        //    //    book.Width = 970;
        //    //    book.DownZipUrl = child.zippath;
        //    //    book.ShareImg.MouseLeftButtonDown += ShareImg_MouseDown;
        //    //    book.DownImg.MouseLeftButtonDown += DownImg_MouseDown;
        //    //    book.EditImg.MouseLeftButtonDown += EditImg_MouseDown;
        //    //    book.DelImg.MouseLeftButtonDown += DelImg_MouseDown;
        //    //    book.UpImg.MouseLeftButtonDown += UpImg_MouseDown;
        //    //    mySCDock.Children.Add(book);

        //    //}

        //}

        /// <summary>
        /// 置顶
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit bookbtn2 = new mybkBookEdit();
            mybkBookEdit4 bookbtn = new mybkBookEdit4();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;

                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn2 = __do.Parent as mybkBookEdit;
                    //ParentView.Update0();
                    break;
                }
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit4")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as mybkBookEdit4;
                    //ParentView.Update0();
                    break;
                }

                if (__do.Parent == null) break;
            }
            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                attendClass.SetBkResourceWeigh(Convert.ToInt32(bookbtn2.Id));
            }
            if (!string.IsNullOrWhiteSpace(bookbtn.Id))
            {
                attendClass.SetBkResourceWeigh(Convert.ToInt32(bookbtn.Id));
            }
        }
        /// <summary>
        /// 上课
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void shangkeImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;

            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit bookbtn2 = new mybkBookEdit();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;

                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn2 = __do.Parent as mybkBookEdit;
                    //ParentView.Update0();
                    break;
                }

                if (__do.Parent == null) break;
            }
            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                attendClass.BeginClass(bookbtn2.Id);
            }
        }
        /// <summary>
        /// 备课包引用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void quoteImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit bookbtn2 = new mybkBookEdit();
            mybkBookEdit4 bookbtn = new mybkBookEdit4();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;

                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn2 = __do.Parent as mybkBookEdit;
                    //ParentView.Update0();
                    break;
                }
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit4")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as mybkBookEdit4;
                    //ParentView.Update0();
                    break;
                }

                if (__do.Parent == null) break;
            }
            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                attendClass.BkResourcesQuote(Convert.ToInt32(bookbtn2.Id));
            }
            if (!string.IsNullOrWhiteSpace(bookbtn.Id))
            {
                attendClass.BkResourcesQuote(Convert.ToInt32(bookbtn.Id));
            }


        }
        /// <summary>
        /// 备课包分享
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShareImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;

            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit bookbtn2 = new mybkBookEdit();
            mybkBookEdit4 bookbtn = new mybkBookEdit4();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;

                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn2 = __do.Parent as mybkBookEdit;
                    //ParentView.Update0();
                    break;
                }
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit4")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as mybkBookEdit4;
                    //ParentView.Update0();
                    break;
                }


                if (__do.Parent == null) break;
            }


            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                bg_addStu.Visibility = Visibility.Visible;
                BkShareId = Convert.ToInt32(bookbtn2.Id);
                //attendClass.SetResourcesShare(Convert.ToInt32(bookbtn2.Id));
            }
            if (!string.IsNullOrWhiteSpace(bookbtn.Id))
            {
                bg_addStu.Visibility = Visibility.Visible;
                BkShareId = Convert.ToInt32(bookbtn.Id);
                //attendClass.SetResourcesShare(Convert.ToInt32(bookbtn2.Id));
            }

        }
        private void DownImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ShowDialogView3 showDialogView = new ShowDialogView3();
            showDialogView.ShowDialogContent = "是否下载该资源包";
            if (showDialogView.ShowDialog() == true)
            {
                AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
                var button = sender;
                FrameworkElement __do = sender as FrameworkElement;
                mybkBookEdit bookbtn = new mybkBookEdit();
                mybkBookEdit4 bookbtn2 = new mybkBookEdit4();
                while (__do != null)
                {
                    __do = __do.Parent as FrameworkElement;
                    if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                    {
                        //AttendClassViewModel Context = __do.DataContext as ;
                        bookbtn = __do.Parent as mybkBookEdit;
                        //ParentView.Update0();
                        break;
                    }
                    if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit4")
                    {
                        //AttendClassViewModel Context = __do.DataContext as ;
                        bookbtn2 = __do.Parent as mybkBookEdit4;
                        //ParentView.Update0();
                        break;
                    }

                    if (__do.Parent == null) break;
                }

                if (!string.IsNullOrWhiteSpace(bookbtn.Id))
                {
                    attendClass.DownBkResource(Convert.ToInt32(bookbtn.Id), bookbtn.DownZipUrl);
                }
                if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
                {
                    attendClass.DownBkResource(Convert.ToInt32(bookbtn2.Id), bookbtn2.DownZipUrl);
                }
            }
        }
        private void EditImg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var button = sender;

        }
        private void DelImg_MouseDown(object sender, MouseButtonEventArgs e)
        {

            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
            var button = sender;
            FrameworkElement __do = sender as FrameworkElement;
            mybkBookEdit bookbtn = new mybkBookEdit();
            mybkBookEdit4 bookbtn2 = new mybkBookEdit4();
            while (__do != null)
            {
                __do = __do.Parent as FrameworkElement;
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn = __do.Parent as mybkBookEdit;
                    //ParentView.Update0();
                    break;
                }
                if (__do.Parent.ToString() == "HuiZe.education.Resource.Controls.mybkBookEdit4")
                {
                    //AttendClassViewModel Context = __do.DataContext as ;
                    bookbtn2 = __do.Parent as mybkBookEdit4;
                    //ParentView.Update0();
                    break;
                }

                if (__do.Parent == null) break;
            }

            if (!string.IsNullOrWhiteSpace(bookbtn.Id))
            {
                attendClass.DelBclass(Convert.ToInt32(bookbtn.Id));
                Update("default", 1);
            }

            if (!string.IsNullOrWhiteSpace(bookbtn2.Id))
            {
                attendClass.DelBclass(Convert.ToInt32(bookbtn2.Id));
                Update("default", 1);
            }
        }

        private void LoginView_Loaded(object sender, RoutedEventArgs e)
        {
            _page_count_last = 0;
            _type_sort = "default";
            AttendClassView = DataContext as AttendClassViewModel;
            pageManage.LoadDataCallback = Update;
            PART_MyPackages_H.DownloadCallback = Download;
            PART_MyPackages_H.DeleteCallback = Delete;
            PART_MyPackages_H.ShareCallback = Share;
            PART_MyPackages_H.EditCallback = Edit;
            PART_MyPackages_H.TopCallback = Top;
            PART_MyPackages_H.LessonCallback = Lesson;

            PART_MyPackages_V.DownloadCallback = Download;
            PART_MyPackages_V.DeleteCallback = Delete;
            PART_MyPackages_V.ShareCallback = Share;
            PART_MyPackages_V.EditCallback = Edit;
            PART_MyPackages_V.TopCallback = Top;
            PART_MyPackages_V.LessonCallback = Lesson;

            Update(_type_sort, 1);
            //pageManage.ItemSourceCount = AttendClassView.pageCount;
            //ControlType = 1;

        }

        private void CustomButton_Click(object sender, RoutedEventArgs e)
        {
            AttendClassViewModel attendClass = DataContext as AttendClassViewModel;
            int id = Convert.ToInt32(CommonMethods.nowId);
            var a = attendClass.DelBclass(id);
            Update("default", 1);
        }

        private void CustomButton_Click_1(object sender, RoutedEventArgs e)
        {
            personPopup.IsOpen = true;
            //Update("default", 1);
        }

        private void CustomToggleButton_Click(object sender, RoutedEventArgs e)
        {
            if (PART_DisplayType.IsChecked == true)
            {
                PART_MyPackages_H.Visibility = Visibility.Visible;
                PART_MyPackages_V.Visibility = Visibility.Collapsed;
                PART_ScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
            }
            else
            {
                PART_MyPackages_H.Visibility = Visibility.Collapsed;
                PART_MyPackages_V.Visibility = Visibility.Visible;
                PART_ScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
            }
            //if (ControlType == 1)
            //{
            //    ControlType = 2;
            //    Update2("default");
            //}
            //else
            //{
            //    ControlType = 1;
            //    Update("default", 1);
            //}

        }

        private void YCButton_Click(object sender, RoutedEventArgs e)
        {
            personPopup.IsOpen = false;
            YCButton yCButton = sender as YCButton;
            string content = yCButton.Content.ToString();
            if (content == "默认")
            {
                _type_sort = "default";
            }
            else if (content == "时间")
            {
                _type_sort = "time";
            }
            else
            {
                _type_sort = "class_sort";
            }
            Update(1);
        }

        private void YCWateTextBox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //Update("default");
        }

        private void exitAddStu_Click(object sender, RoutedEventArgs e)
        {
            bg_addStu.Visibility = Visibility.Collapsed;
        }

        private void YCButton_Click_1(object sender, RoutedEventArgs e)
        {
            bg_addStu.Visibility = Visibility.Collapsed;
        }

        private void YCButton_Click_2(object sender, RoutedEventArgs e)
        {
            AttendClassView.SetResourcesShare(BkShareId);
            bg_addStu.Visibility = Visibility.Collapsed;
        }

        private void YCWateTextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource.ToString().Contains("搜索"))
            {
                AttendClassView.searchBkName = PART_Search.Text;
                Update(1);
            }
        }

        private void PageManage_PreviewMouseUp_1(object sender, MouseButtonEventArgs e)
        {
            //if (e.OriginalSource.ToString().Contains("CustomRadioButton"))
            //{
            //    CustomRadioButton button = e.OriginalSource as CustomRadioButton;
            //    string index = button.Content.ToString();
            //    if (index == ">")
            //    {
            //        //index = pageManage.Content.ToString();
            //        index = pageManage.CurrentIndex < pageManage.ItemSourceCount ? (pageManage.CurrentIndex + 1).ToString() : pageManage.CurrentIndex.ToString();
            //    }
            //    if (index == "<")
            //    {
            //        index = pageManage.CurrentIndex > 1 ? (pageManage.CurrentIndex - 1).ToString() : "1";
            //    }
            //    if (index == "首页")
            //    {
            //        index = "1";
            //    }
            //    if (index == "尾页")
            //    {
            //        index = pageManage.ItemSourceCount.ToString();
            //    }
            //    AttendClassView.GetMyBkResourceList("default", Convert.ToInt32(index));
            //}
        }

        void Download(ResMyBkResourceDetailItemModel _data)
        {
            ShowDialogView3 showDialogView = new ShowDialogView3();
            showDialogView.ShowDialogContent = "是否下载该资源包";
            if (showDialogView.ShowDialog() == true)
            {
                AttendClassView.DownBkResource(_data.id, _data.zippath);
            }
        }
        void Delete(ResMyBkResourceDetailItemModel _data)
        {
            ShowDialogView3 showDialogView = new ShowDialogView3();
            showDialogView.ShowDialogContent = "确定要删除 ?";
            if (showDialogView.ShowDialog() == true)
            {
                if (AttendClassView.DelBclass(_data.id))
                    Update(1);
            }
        }
        void Share(ResMyBkResourceDetailItemModel _data)
        {
            bg_addStu.Visibility = Visibility.Visible;
            BkShareId = _data.id;
        }
        void Edit(ResMyBkResourceDetailItemModel _data)
        {
            //AttendClassView.NavChangedComand.Execute("PlanLessonView");

            var _locator = (ViewModelLocator)App.Current.FindResource("Locator");
            _locator.PlanLessonView.ShowResDetailId = _data.id.ToString();
            _locator.PlanLessonView.IsShowingSelectedBackageItem = true;
            _locator.PlanLessonView.CurrentEditResDetailID = _data.id;
            _locator.MainView.NavChanged("PlanLessonView");

            //SelectChapterView resourceView = new SelectChapterView() { DataContext = new PlanLessonViewModel() };
            //resourceView.Owner = App.Current.MainWindow;
            //resourceView.ShowDialog();
            //if (resourceView.ShowDialogResult)
            //{

            //}
        }
        void Top(ResMyBkResourceDetailItemModel _data)
        {
            if (AttendClassView.SetBkResourceWeigh(_data.id))
            {
                PART_SortType_Default.IsChecked = new bool?(true);
                //Update(1);
            }
        }
        void Lesson(ResMyBkResourceDetailItemModel _data)
        {
            ShowDialogComboxView cb = new ShowDialogComboxView();
            cb.Owner = MainView.Instance;
            cb.ShowInTaskbar = false;
            cb.ShowDialog();
            if (cb.DialogResult == false)
            {
                return;
            }
            AttendClassView.BeginClassExtrend(_data.title,_data.cat_name,_data.id.ToString(),cb.SelectClass);
        }

        string _type_sort;
        int _page_count_last;
    }
}
